package elicecn.web.util;

/**
 * 人民币大写转换
 */
public class CurrencyTransfer {
	public static final int INTEGER = 1;
	public static final int DECIMAL_FRACTION = 2;
	protected static final String NUMBER[] = { "零", "壹", "贰", "叁", "肆", "伍",
			"陆", "柒", "捌", "玖" };
	protected static final String UNIT[] = { "元", "拾", "佰", "仟", "萬", "拾", "佰",
			"仟", "億", "拾", "佰", "仟", "萬", "拾", "佰", "仟" };
	protected static final String SMALL_UNIT[] = { "分", "角" };
	protected static final String END = "整";

	/**
	 * 
	 * @param flag
	 *            标志：整数INTEGER或小数DECIMAL_FRACTION
	 * @param str
	 * @return
	 */
	protected static String format(int flag, String str) {
		int len = str.length();
		String currency = "";
		if (flag == 1) {
			for (int i = 0; i < len; i++) {
				currency = currency + NUMBER[str.charAt(i) - 48]
						+ UNIT[len - i - 1];
			}
		} else if (flag == 2) {
			for (int i = 0; i < len; i++) {
				currency = currency + NUMBER[str.charAt(i) - 48]
						+ SMALL_UNIT[len - i - 1];
			}
		}
		return currency;
	}

	public static String format(String str) {
		String currency[] = str.split("[.]");
		if (currency.length == 1) {
			return format(INTEGER, currency[0]) + END;
		} else if (currency.length == 2) {
			if (currency[1].length() == 1) {
				currency[1] += "0";
			}
			return format(INTEGER, currency[0])
					+ format(DECIMAL_FRACTION, currency[1]) + END;
		} else {
			throw new IllegalArgumentException("格式错误：" + str);
		}

	}

	public static void main(String[] a) {
		System.out.println(format("123456789.00"));
	}

}
